﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class mydesk : System.Web.UI.Page
{


    string s_where, s_order;
    string curindex = String.Empty;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Session["sTittle"] = "学生专区>>我的课桌";
            string zxd1 = "";
            string noth = Session["sKecheng"].ToString();
            zxd1 = zxdtransform(noth);
            string zxd2 = "";
            noth = Session["sKechenged"].ToString();
            zxd2 = zxdtransform(noth);
            txtzxd1.Text = " WHERE " + zxd1;
            txtzxd2.Text = " WHERE " + zxd2;
            //SqlDataSource1.SelectCommand = txtzxd1.Text.Trim();
            HL1.Visible = true;
            HL2.Visible = false;
            HL4.Visible = false;
            DataBrid();
            
        }
    }

    //绑定数据
    private void DataBrid()
    {
        int record = 0;
        int count = 0;
        s_order = "ORDER BY [id] DESC";
        s_where = string.Format(" where {0}", zxdtransform(Session["sKecheng"].ToString()));
        Session["sqlwhere"] = s_where;
        string sqlText = " [id], [name],[laoshi]";
        DataTable DT = DbHelper.ExecutePage(sqlText, "[kecheng]" + s_where, "id", s_order, Pager.CurrentPageIndex, Pager.PageSize, out record, out count, null);
        Pager.RecordCount = record;
        this.GridView1.DataSource = DT;
        this.GridView1.DataBind();
    }



    //李红新更改 2010-09-17 条件绑定数据
    private void DataBrid(string sqlwhere)
    {
        int record = 0;
        int count = 0;
        s_order = "ORDER BY [id] DESC";
        string sqlText = " [id], [name],[laoshi]";
        DataTable DT = DbHelper.ExecutePage(sqlText, "[kecheng]" + sqlwhere, "id", s_order, Pager.CurrentPageIndex, Pager.PageSize, out record, out count, null);
        Pager.RecordCount = record;
        this.GridView1.DataSource = DT;
        this.GridView1.DataBind();
    }



    protected string zxdtransform(string zs1)   //由 Session["sKecheng"], Session["sKechenged"], Session["sKecheng_Voted"] 此方法还会用在学生已学章节上.
    {
        string zxdstring="[id]=";
        //string zs1 = Session["sKecheng"].ToString();
        if (zs1.Length>=2)    //如果参数zs1是空的，则返回“ ”，并退出程序。否则生成形如id=id1...id=id?的字符串
        {
        zs1 = zs1.Substring(2); //去掉前2个“,,”。
        }
        else
        {
            return "1=2";
        }
        int i=zs1.IndexOf(",,");
        while (i > 0)
        {
            zxdstring = zxdstring + zs1.Substring(0, i) + " OR [id]=";
            zs1 = zs1.Substring(i + 2);
            i = zs1.IndexOf(",,");
        }
        i = zs1.IndexOf(",");
        zxdstring = zxdstring + zs1.Substring(0, i);
        return "(" + zxdstring + ") ";

    }


    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            LinkButton lbt = e.Row.FindControl("LinkButton1") as LinkButton;
            if (lbt != null)
            {
                lbt.OnClientClick = string.Format("openurl({0}); return false;", DataBinder.Eval(e.Row.DataItem, "id"));
            }
            LinkButton lbt2 = e.Row.FindControl("LinkButton2") as LinkButton;
            if(lbt2 != null)
            {
                lbt2.OnClientClick = string.Format("jumpurl({0}); return false;", DataBinder.Eval(e.Row.DataItem, "id"));
            }
        }
        if (e.Row.RowIndex != -1)
        {
            e.Row.Cells[0].Text = (e.Row.RowIndex + (Pager.PageSize * (Pager.CurrentPageIndex - 1) + 1)).ToString();
        }
    }


    protected void LB1_Click(object sender, EventArgs e)
    {
        //正在学习的课程
        HL1.Visible = true;
        HL2.Visible = false;
        HL4.Visible = false;
        GridView1.PageIndex = 0;
        //SqlDataSource1.SelectCommand = txtzxd1.Text.Trim();
        GridView1.Columns[4].Visible = true;
        ViewState["sqlwhere"] = txtzxd1.Text;
        DataBrid(txtzxd1.Text);
    }
    protected void LB2_Click(object sender, EventArgs e)
    {
        //已通过的课程
        HL1.Visible = false;
        HL2.Visible = true;
        HL4.Visible = false;
        GridView1.PageIndex = 0;
        //SqlDataSource1.SelectCommand = txtzxd2.Text.Trim();
        GridView1.Columns[4].Visible = false;
        ViewState["sqlwhere"] = txtzxd2.Text;
        DataBrid(txtzxd2.Text);
    }
    protected void LB4_Click(object sender, EventArgs e)
    {
        //未通过审核的课程
        HL1.Visible = false;
        HL2.Visible = false;
        HL4.Visible = true;
        GridView1.PageIndex = 0;
        //SqlDataSource1.SelectCommand = txtzxd3.Text;
        //GridView1.Columns[2].Visible = false;
        ViewState["sqlwhere"] = txtzxd3.Text;
        DataBrid(txtzxd3.Text);
    }


    protected void pager_PageChanging(object src, Wuqi.Webdiyer.PageChangingEventArgs e)
    {
        Pager.CurrentPageIndex = e.NewPageIndex;
        if (ViewState["sqlwhere"] != null)
        {
            DataBrid(ViewState["sqlwhere"].ToString());
        }
        else
        {
            DataBrid();
        }

    }



    protected void Button1_Click(object sender, EventArgs e)
    {
        string strWhere = Session["sqlwhere"].ToString();
        if (!string.IsNullOrEmpty(KeChengMingCheng.Text))
        {
            strWhere += " and name like '%" + KeChengMingCheng.Text + "%'";
        }
        if (!string.IsNullOrEmpty(ShouKeLaoShi.Text))
        {
            strWhere += " and laoshi like '%" + ShouKeLaoShi.Text + "%'";
        }
        if (string.IsNullOrEmpty(KeChengMingCheng.Text) && string.IsNullOrEmpty(ShouKeLaoShi.Text))
        {
            Pager.CurrentPageIndex = 0;
            DataBrid();
        }
        else
        {
            Pager.CurrentPageIndex = 0;
            ViewState["sqlwhere"] = strWhere;
            DataBrid(ViewState["sqlwhere"].ToString());
        }
    }
}
